package injections;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by rehak on 2/11/14.
 */
public class DefinedClass {

    private volatile String instanceName;
    private final String className;
    private final List<DefinedProperty> properties = new CopyOnWriteArrayList<>();
    private final List<DefinedProperty> constructorProps = new CopyOnWriteArrayList<>();

    public DefinedClass(String instanceName, String className, Collection<DefinedProperty> properties, Collection<DefinedProperty> constructorProperties) {
        this.instanceName = instanceName;
        this.className = className;
        if (properties != null) {
            this.properties.addAll(properties);
        }
        if (constructorProperties != null) {
            this.constructorProps.addAll(constructorProperties);
        }
    }

    public String getInstanceName() {
        return instanceName;
    }

    public String getClassName() {
        return className;
    }

    public List<DefinedProperty> getConstructorProps() {
        return Collections.unmodifiableList(constructorProps);
    }

    public List<DefinedProperty> getProperties() {
        return Collections.unmodifiableList(properties);
    }

    public DefinedProperty getConstructorProperty(String propName) {
        return findProp(constructorProps, propName);
    }

    public DefinedProperty getProperty(String propName) {
        return findProp(properties, propName);
    }

    private static DefinedProperty findProp(Collection<DefinedProperty> col, String propName) {
        for (DefinedProperty p : col) {
            if (p.getName().equals(propName)) {
                return p;
            }
        }
        return null;
    }

    public void setInstanceName(String instanceName) {
        this.instanceName = instanceName;
    }
}
